home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / rhythmbox / plugins / python-console / pythonconsole.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  15.0 KB  |  391 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import string
  5. import sys
  6. import re
  7. import traceback
  8. import gobject
  9. import gtk
  10. import pango
  11. import gconf
  12. import rhythmdb
  13. import rb
  14.  
  15. try:
  16.     import rpdb2
  17.     have_rpdb2 = True
  18. except:
  19.     have_rpdb2 = False
  20.  
  21. ui_str = '\n<ui>\n  <menubar name="MenuBar">\n    <menu name="ToolsMenu" action="Tools">\n      <placeholder name="ToolsOps_5">\n        <menuitem name="PythonConsole" action="PythonConsole"/>\n        <menuitem name="PythonDebugger" action="PythonDebugger"/>\n      </placeholder>\n    </menu>\n  </menubar>\n</ui>\n'
  22.  
  23. class PythonConsolePlugin(rb.Plugin):
  24.     
  25.     def __init__(self):
  26.         rb.Plugin.__init__(self)
  27.         self.window = None
  28.  
  29.     
  30.     def activate(self, shell):
  31.         data = dict()
  32.         manager = shell.get_player().get_property('ui-manager')
  33.         data['action_group'] = gtk.ActionGroup('PythonConsolePluginActions')
  34.         action = gtk.Action('PythonConsole', _('_Python Console'), _("Show Rhythmbox's python console"), 'gnome-mime-text-x-python')
  35.         action.connect('activate', self.show_console, shell)
  36.         data['action_group'].add_action(action)
  37.         action = gtk.Action('PythonDebugger', _('Python Debugger'), _('Enable remote python debugging with rpdb2'), None)
  38.         if have_rpdb2:
  39.             action.connect('activate', self.enable_debugging, shell)
  40.         else:
  41.             action.set_visible(False)
  42.         data['action_group'].add_action(action)
  43.         manager.insert_action_group(data['action_group'], 0)
  44.         data['ui_id'] = manager.add_ui_from_string(ui_str)
  45.         manager.ensure_update()
  46.         shell.set_data('PythonConsolePluginInfo', data)
  47.  
  48.     
  49.     def deactivate(self, shell):
  50.         data = shell.get_data('PythonConsolePluginInfo')
  51.         manager = shell.get_player().get_property('ui-manager')
  52.         manager.remove_ui(data['ui_id'])
  53.         manager.remove_action_group(data['action_group'])
  54.         manager.ensure_update()
  55.         shell.set_data('PythonConsolePluginInfo', None)
  56.         if self.window is not None:
  57.             self.window.destroy()
  58.         
  59.  
  60.     
  61.     def show_console(self, action, shell):
  62.         if not self.window:
  63.             ns = {
  64.                 '__builtins__': __builtins__,
  65.                 'rb': rb,
  66.                 'rhythmdb': rhythmdb,
  67.                 'shell': shell }
  68.             console = PythonConsole(namespace = ns, destroy_cb = self.destroy_console)
  69.             console.set_size_request(600, 400)
  70.             console.eval('print "' + _("You can access the main window through the 'shell' variable :") + '\\n%s" % shell', False)
  71.             self.window = gtk.Window()
  72.             self.window.set_title('Rhythmbox Python Console')
  73.             self.window.add(console)
  74.             self.window.connect('destroy', self.destroy_console)
  75.             self.window.show_all()
  76.         else:
  77.             self.window.show_all()
  78.         self.window.grab_focus()
  79.  
  80.     
  81.     def enable_debugging(self, action, shell):
  82.         msg = _("After you press OK, Rhythmbox will wait until you connect to it with winpdb or rpdb2. If you have not set a debugger password in GConf, it will use the default password ('rhythmbox').")
  83.         dialog = gtk.MessageDialog(None, 0, gtk.MESSAGE_INFO, gtk.BUTTONS_OK_CANCEL, msg)
  84.         if dialog.run() == gtk.RESPONSE_OK:
  85.             gconfclient = gconf.client_get_default()
  86.             if not gconfclient.get_string('/apps/rhythmbox/plugins/pythonconsole/rpdb2_password'):
  87.                 pass
  88.             password = 'rhythmbox'
  89.             
  90.             def start_debugger(password):
  91.                 rpdb2.start_embedded_debugger(password)
  92.                 return False
  93.  
  94.             gobject.idle_add(start_debugger, password)
  95.         
  96.         dialog.destroy()
  97.  
  98.     
  99.     def destroy_console(self, *args):
  100.         self.window.destroy()
  101.         self.window = None
  102.  
  103.  
  104.  
  105. class PythonConsole(gtk.ScrolledWindow):
  106.     
  107.     def __init__(self, namespace = { }, destroy_cb = None):
  108.         gtk.ScrolledWindow.__init__(self)
  109.         self.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
  110.         self.set_shadow_type(gtk.SHADOW_IN)
  111.         self.view = gtk.TextView()
  112.         self.view.modify_font(pango.FontDescription('Monospace'))
  113.         self.view.set_editable(True)
  114.         self.view.set_wrap_mode(gtk.WRAP_WORD_CHAR)
  115.         self.add(self.view)
  116.         self.view.show()
  117.         buffer = self.view.get_buffer()
  118.         self.normal = buffer.create_tag('normal')
  119.         self.error = buffer.create_tag('error')
  120.         self.error.set_property('foreground', 'red')
  121.         self.command = buffer.create_tag('command')
  122.         self.command.set_property('foreground', 'blue')
  123.         self._PythonConsole__spaces_pattern = re.compile('^\\s+')
  124.         self.namespace = namespace
  125.         self.destroy_cb = destroy_cb
  126.         self.block_command = False
  127.         buffer.create_mark('input-line', buffer.get_end_iter(), True)
  128.         buffer.insert(buffer.get_end_iter(), '>>> ')
  129.         buffer.create_mark('input', buffer.get_end_iter(), True)
  130.         self.history = [
  131.             '']
  132.         self.history_pos = 0
  133.         self.current_command = ''
  134.         self.namespace['__history__'] = self.history
  135.         self.stdout = gtkoutfile(self, sys.stdout.fileno(), self.normal)
  136.         self.stderr = gtkoutfile(self, sys.stderr.fileno(), self.error)
  137.         self.view.connect('key-press-event', self._PythonConsole__key_press_event_cb)
  138.         buffer.connect('mark-set', self._PythonConsole__mark_set_cb)
  139.  
  140.     
  141.     def __key_press_event_cb(self, view, event):
  142.         if event.keyval == gtk.keysyms.d and event.state == gtk.gdk.CONTROL_MASK:
  143.             self.destroy()
  144.         elif event.keyval == gtk.keysyms.Return and event.state == gtk.gdk.CONTROL_MASK:
  145.             buffer = view.get_buffer()
  146.             inp_mark = buffer.get_mark('input')
  147.             inp = buffer.get_iter_at_mark(inp_mark)
  148.             cur = buffer.get_end_iter()
  149.             line = buffer.get_text(inp, cur)
  150.             self.current_command = self.current_command + line + '\n'
  151.             self.history_add(line)
  152.             cur = buffer.get_end_iter()
  153.             buffer.insert(cur, '\n... ')
  154.             cur = buffer.get_end_iter()
  155.             buffer.move_mark(inp_mark, cur)
  156.             spaces = re.match(self._PythonConsole__spaces_pattern, line)
  157.             if spaces is not None:
  158.                 buffer.insert(cur, line[spaces.start():spaces.end()])
  159.                 cur = buffer.get_end_iter()
  160.             
  161.             buffer.place_cursor(cur)
  162.             gobject.idle_add(self.scroll_to_end)
  163.             return True
  164.         if event.keyval == gtk.keysyms.Return:
  165.             buffer = view.get_buffer()
  166.             lin_mark = buffer.get_mark('input-line')
  167.             inp_mark = buffer.get_mark('input')
  168.             inp = buffer.get_iter_at_mark(inp_mark)
  169.             cur = buffer.get_end_iter()
  170.             line = buffer.get_text(inp, cur)
  171.             self.current_command = self.current_command + line + '\n'
  172.             self.history_add(line)
  173.             lin = buffer.get_iter_at_mark(lin_mark)
  174.             buffer.apply_tag(self.command, lin, cur)
  175.             buffer.insert(cur, '\n')
  176.             cur_strip = self.current_command.rstrip()
  177.             if (cur_strip.endswith(':') or self.current_command[-2:] != '\n\n') and self.block_command:
  178.                 self.block_command = True
  179.                 com_mark = '... '
  180.             elif cur_strip.endswith('\\'):
  181.                 com_mark = '... '
  182.             else:
  183.                 self._PythonConsole__run(self.current_command)
  184.                 self.current_command = ''
  185.                 self.block_command = False
  186.                 com_mark = '>>> '
  187.             cur = buffer.get_end_iter()
  188.             buffer.move_mark(lin_mark, cur)
  189.             buffer.insert(cur, com_mark)
  190.             cur = buffer.get_end_iter()
  191.             buffer.move_mark(inp_mark, cur)
  192.             buffer.place_cursor(cur)
  193.             gobject.idle_add(self.scroll_to_end)
  194.             return True
  195.         if event.keyval == gtk.keysyms.KP_Down or event.keyval == gtk.keysyms.Down:
  196.             view.emit_stop_by_name('key_press_event')
  197.             self.history_down()
  198.             gobject.idle_add(self.scroll_to_end)
  199.             return True
  200.         if event.keyval == gtk.keysyms.KP_Up or event.keyval == gtk.keysyms.Up:
  201.             view.emit_stop_by_name('key_press_event')
  202.             self.history_up()
  203.             gobject.idle_add(self.scroll_to_end)
  204.             return True
  205.         if event.keyval == gtk.keysyms.KP_Left and event.keyval == gtk.keysyms.Left or event.keyval == gtk.keysyms.BackSpace:
  206.             buffer = view.get_buffer()
  207.             inp = buffer.get_iter_at_mark(buffer.get_mark('input'))
  208.             cur = buffer.get_iter_at_mark(buffer.get_insert())
  209.             return inp.compare(cur) == 0
  210.         if event.keyval == gtk.keysyms.Home:
  211.             buffer = view.get_buffer()
  212.             inp = buffer.get_iter_at_mark(buffer.get_mark('input'))
  213.             return True
  214.  
  215.     
  216.     def __mark_set_cb(self, buffer, iter, name):
  217.         input = buffer.get_iter_at_mark(buffer.get_mark('input'))
  218.         pos = buffer.get_iter_at_mark(buffer.get_insert())
  219.         self.view.set_editable(pos.compare(input) != -1)
  220.  
  221.     
  222.     def get_command_line(self):
  223.         buffer = self.view.get_buffer()
  224.         inp = buffer.get_iter_at_mark(buffer.get_mark('input'))
  225.         cur = buffer.get_end_iter()
  226.         return buffer.get_text(inp, cur)
  227.  
  228.     
  229.     def set_command_line(self, command):
  230.         buffer = self.view.get_buffer()
  231.         mark = buffer.get_mark('input')
  232.         inp = buffer.get_iter_at_mark(mark)
  233.         cur = buffer.get_end_iter()
  234.         buffer.delete(inp, cur)
  235.         buffer.insert(inp, command)
  236.         buffer.select_range(buffer.get_iter_at_mark(mark), buffer.get_end_iter())
  237.         self.view.grab_focus()
  238.  
  239.     
  240.     def history_add(self, line):
  241.         if line.strip() != '':
  242.             self.history_pos = len(self.history)
  243.             self.history[self.history_pos - 1] = line
  244.             self.history.append('')
  245.         
  246.  
  247.     
  248.     def history_up(self):
  249.         if self.history_pos > 0:
  250.             self.history[self.history_pos] = self.get_command_line()
  251.             self.history_pos = self.history_pos - 1
  252.             self.set_command_line(self.history[self.history_pos])
  253.         
  254.  
  255.     
  256.     def history_down(self):
  257.         if self.history_pos < len(self.history) - 1:
  258.             self.history[self.history_pos] = self.get_command_line()
  259.             self.history_pos = self.history_pos + 1
  260.             self.set_command_line(self.history[self.history_pos])
  261.         
  262.  
  263.     
  264.     def scroll_to_end(self):
  265.         iter = self.view.get_buffer().get_end_iter()
  266.         self.view.scroll_to_iter(iter, 0)
  267.         return False
  268.  
  269.     
  270.     def write(self, text, tag = None):
  271.         buffer = self.view.get_buffer()
  272.         if tag is None:
  273.             buffer.insert(buffer.get_end_iter(), text)
  274.         else:
  275.             buffer.insert_with_tags(buffer.get_end_iter(), text, tag)
  276.         gobject.idle_add(self.scroll_to_end)
  277.  
  278.     
  279.     def eval(self, command, display_command = False):
  280.         buffer = self.view.get_buffer()
  281.         lin = buffer.get_mark('input-line')
  282.         buffer.delete(buffer.get_iter_at_mark(lin), buffer.get_end_iter())
  283.         if isinstance(command, list) or isinstance(command, tuple):
  284.             for c in command:
  285.                 if display_command:
  286.                     self.write('>>> ' + c + '\n', self.command)
  287.                 
  288.                 self._PythonConsole__run(c)
  289.             
  290.         elif display_command:
  291.             self.write('>>> ' + c + '\n', self.command)
  292.         
  293.         self._PythonConsole__run(command)
  294.         cur = buffer.get_end_iter()
  295.         buffer.move_mark_by_name('input-line', cur)
  296.         buffer.insert(cur, '>>> ')
  297.         cur = buffer.get_end_iter()
  298.         buffer.move_mark_by_name('input', cur)
  299.         self.view.scroll_to_iter(buffer.get_end_iter(), 0)
  300.  
  301.     
  302.     def __run(self, command):
  303.         sys.stdout = self.stdout
  304.         self.stdout = sys.stdout
  305.         sys.stderr = self.stderr
  306.         self.stderr = sys.stderr
  307.         
  308.         try:
  309.             
  310.             try:
  311.                 r = eval(command, self.namespace, self.namespace)
  312.                 if r is not None:
  313.                     print `r`
  314.             except SyntaxError:
  315.                 exec command in self.namespace
  316.  
  317.         except:
  318.             if hasattr(sys, 'last_type') and sys.last_type == SystemExit:
  319.                 self.destroy()
  320.             else:
  321.                 traceback.print_exc()
  322.  
  323.         sys.stdout = self.stdout
  324.         self.stdout = sys.stdout
  325.         sys.stderr = self.stderr
  326.         self.stderr = sys.stderr
  327.  
  328.     
  329.     def destroy(self):
  330.         if self.destroy_cb is not None:
  331.             self.destroy_cb()
  332.         
  333.  
  334.  
  335.  
  336. class gtkoutfile:
  337.     '''A fake output file object.  It sends output to a TK test widget,
  338. \tand if asked for a file number, returns one set on instance creation'''
  339.     
  340.     def __init__(self, console, fn, tag):
  341.         self.fn = fn
  342.         self.console = console
  343.         self.tag = tag
  344.  
  345.     
  346.     def close(self):
  347.         pass
  348.  
  349.     
  350.     def flush(self):
  351.         pass
  352.  
  353.     
  354.     def fileno(self):
  355.         return self.fn
  356.  
  357.     
  358.     def isatty(self):
  359.         return 0
  360.  
  361.     
  362.     def read(self, a):
  363.         return ''
  364.  
  365.     
  366.     def readline(self):
  367.         return ''
  368.  
  369.     
  370.     def readlines(self):
  371.         return []
  372.  
  373.     
  374.     def write(self, s):
  375.         self.console.write(s, self.tag)
  376.  
  377.     
  378.     def writelines(self, l):
  379.         self.console.write(l, self.tag)
  380.  
  381.     
  382.     def seek(self, a):
  383.         raise IOError, (29, 'Illegal seek')
  384.  
  385.     
  386.     def tell(self):
  387.         raise IOError, (29, 'Illegal seek')
  388.  
  389.     truncate = tell
  390.  
  391.